{"componentChunkName":"component---src-templates-post-jsx","path":"/[CS]Tree/","result":{"data":{"site":{"siteMetadata":{"title":"SUbbb"}},"markdownRemark":{"id":"81b508c5-2689-54e4-b36f-2e8bd07e2297","excerpt":"🧷 트리 값을 가진 노드(Node)와 이 노드들을 이어주는 간선(Edge)로 이루어진 자료구조이다. 트리라는 이름이 나온 이유? : 실제 나무를 거꾸로 세워놓은 듯한 모양을 보이기 때문이다. 그림에서, 데이터 1을 가진 노드가 루트 노드이다. 모든 노드들은 0개 이상의 자식 노드를 가지고 있으며, \"부모-자식 관계\"로 불린다. 선형 자료구조에서 배열이나 …","html":"<h2>🧷 트리</h2>\n<p>값을 가진 노드(Node)와 이 노드들을 이어주는 간선(Edge)로 이루어진 자료구조이다.</p>\n<blockquote>\n<p><strong>트리라는 이름이 나온 이유?</strong></p>\n<p>: 실제 나무를 거꾸로 세워놓은 듯한 모양을 보이기 때문이다.</p>\n</blockquote>\n<img src='https://www.geeksforgeeks.org/wp-content/uploads/binary-tree-to-DLL.png' width='50%'>\n<ul>\n<li>그림에서, 데이터 1을 가진 노드가 <strong>루트 노드</strong>이다.</li>\n<li>모든 노드들은 0개 이상의 자식 노드를 가지고 있으며, \"부모-자식 관계\"로 불린다.</li>\n</ul>\n<p>선형 자료구조에서 배열이나 리스트 등도 존재하지만, 트리가 나온 이유는 뭘까?</p>\n<ul>\n<li>\n<p>일반 배열에서 삽입이나 삭제를 하는데 <span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>O</mi><mo stretchy=\"false\">(</mo><mi>N</mi><mo stretchy=\"false\">)</mo></mrow><annotation encoding=\"application/x-tex\">O(N)</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1em;vertical-align:-0.25em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.02778em;\">O</span><span class=\"mopen\">(</span><span class=\"mord mathnormal\" style=\"margin-right:0.10903em;\">N</span><span class=\"mclose\">)</span></span></span></span>의 시간이 걸린다. </p>\n<ul>\n<li>배열의 첫번째 원소에 삽입하는 경우 나머지 모든 요소들을 한 칸씩 뒤로 미뤄야 하므로 최악의 경우, <span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>O</mi><mo stretchy=\"false\">(</mo><mi>N</mi><mo stretchy=\"false\">)</mo></mrow><annotation encoding=\"application/x-tex\">O(N)</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1em;vertical-align:-0.25em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.02778em;\">O</span><span class=\"mopen\">(</span><span class=\"mord mathnormal\" style=\"margin-right:0.10903em;\">N</span><span class=\"mclose\">)</span></span></span></span>의 시간 복잡도가 나온다.</li>\n</ul>\n</li>\n<li>하지만 트리는 편향 트리가 아닌 이상, <span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>O</mi><mo stretchy=\"false\">(</mo><mi>l</mi><mi>o</mi><mi>g</mi><mi>N</mi><mo stretchy=\"false\">)</mo></mrow><annotation encoding=\"application/x-tex\">O(logN)</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1em;vertical-align:-0.25em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.02778em;\">O</span><span class=\"mopen\">(</span><span class=\"mord mathnormal\" style=\"margin-right:0.01968em;\">l</span><span class=\"mord mathnormal\">o</span><span class=\"mord mathnormal\" style=\"margin-right:0.03588em;\">g</span><span class=\"mord mathnormal\" style=\"margin-right:0.10903em;\">N</span><span class=\"mclose\">)</span></span></span></span> 정도의 시간으로 줄여진다.</li>\n</ul>\n<h3>🪚 특징</h3>\n<ul>\n<li>Stack이나 Queue와 같은 선형 구조가 아닌 <strong>비선형 자료구조</strong>이다.</li>\n<li>계층적 관계를 표현한다.</li>\n<li>루트 노드를 제외한 모든 노드는 <strong>단 하나의 부모 노드</strong>만을 갖는다.</li>\n<li><strong>트리에는 사이클이 존재할 수 없다.</strong> (만약 사이클이 만들어진다면, 그래프이다.)</li>\n<li>모든 노드는 자료형으로 표현이 가능하다.</li>\n<li>루트에서 한 노드로 가는 경로는 유일한 경로뿐이다.</li>\n<li>노드의 개수가 <strong>N</strong>개면, 간선은 <strong>N-1</strong>개를 가진다.</li>\n</ul>\n<h3>🪚 트리 순회 방식</h3>\n<p>트리의 루트를 기준으로 총 4가지의 순회 방식이 있다. 트리는 위 그림을 사용한다.</p>\n<ol>\n<li>\n<p><strong>전위 순회(pre-order)</strong></p>\n<ul>\n<li>각 루트를 순차적으로 먼저 방문하는 방식이다. (Root -> 왼쪽 자식 -> 오른쪽 자식)</li>\n<li><code class=\"language-text\">1 -> 2 -> 4 -> 8 -> 9 -> 5 -> 10 -> 11 -> 3 -> 6 -> 13 -> 7 -> 14</code></li>\n</ul>\n</li>\n<li>\n<p><strong>중위 순회(in-order)</strong></p>\n<ul>\n<li>왼쪽 하위 트리를 방문한 후 루트를 방문하는 방식이다. (왼쪽 자식 -> Root -> 오른쪽 자식)</li>\n<li><code class=\"language-text\">8 -> 4 -> 9 -> 2 -> 10 -> 5 -> 11 -> 1 -> 6 -> 13 -> 3 -> 14 -> 7</code></li>\n</ul>\n</li>\n<li>\n<p><strong>후위 순회(post-order)</strong></p>\n<ul>\n<li>왼쪽 하위 트리부터 하위를 모두 방문한 후 루트를 방문하는 방식이다. (왼쪽 자식 -> 오른쪽 자식 -> Root)</li>\n<li><code class=\"language-text\">8 -> 9 -> 4 -> 10 -> 11 -> 5 -> 2 -> 13 -> 6 -> 14 -> 7 -> 3 -> 1</code></li>\n</ul>\n</li>\n<li>\n<p><strong>레벨 순회(level-order)</strong></p>\n<ul>\n<li>루트부터 계층별로 방문하는 방식이다.</li>\n<li><code class=\"language-text\">1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> 12 -> 13 -> 14</code></li>\n</ul>\n</li>\n</ol>\n<h3>🪚 구현</h3>\n<p>트리의 구현을 위해서는 아래의 정보들이 필요하다.</p>\n<ol>\n<li>데이터와 연결 상태를 저장할 클래스 공간(=노드) 생성</li>\n<li>각각의 노드들에 값 저장</li>\n<li>노드 간 연결 상태 정의</li>\n</ol>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">class Node {\n  Node left;\n  Node right;\n  String value;\n\n  public Node(String value) {\n    this.left = null;\n    this.right = null;\n    this.value = value;\n  }\n}</code>\n        </deckgo-highlight-code>\n<ul>\n<li>각 노드들은 값과, 왼쪽, 오른쪽 자식 노드를 가진다.</li>\n</ul>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">class Tree {\n  Node root;\n  int size = 0;\n}</code>\n        </deckgo-highlight-code>\n<ul>\n<li>트리는 루트 노드와, 그 크기 정보를 가진다.</li>\n</ul>\n<hr>\n<h2>📕 참고</h2>\n<ul>\n<li><a href=\"https://gyoogle.dev/blog/computer-science/data-structure/Tree.html\">Tech Interview for developer</a></li>\n<li><a href=\"https://github.com/WooVictory/Ready-For-Tech-Interview/blob/master/Data%20Structure/%5BData%20Structure%5D%20Tree.md\">Tree</a></li>\n<li><a href=\"https://readerr.tistory.com/35\">[자료구조, Java] 트리(Tree) 개념 정리 및 구현</a></li>\n</ul>","frontmatter":{"title":"📂 Tree(트리)","date":"May 27, 2022","update":"May 27, 2022","tags":["개발상식","Java","Tree"],"series":"📂 Computer Science"},"fields":{"slug":"/[CS]Tree/","readingTime":{"minutes":4.365}}},"seriesList":{"edges":[{"node":{"id":"3fe44622-13ba-589a-bbe8-01581ea7eb50","fields":{"slug":"/[CS]배열/"},"frontmatter":{"title":"📂 배열 (Array)"}}},{"node":{"id":"5b487c3d-e474-5a07-a26c-fc8c7d7cab22","fields":{"slug":"/[CS]연결리스트/"},"frontmatter":{"title":"📂 연결 리스트 (Linked List)"}}},{"node":{"id":"57149a6b-c730-5ce4-a3c5-9cea44979c8c","fields":{"slug":"/[CS]Array&ArrayList&LinkedList/"},"frontmatter":{"title":"📂 Array vs. ArrayList vs. LinkedList"}}},{"node":{"id":"28dac540-50bf-57df-afd9-9561888e8889","fields":{"slug":"/[CS]Stack&Queue/"},"frontmatter":{"title":"📂 Stack & Queue"}}},{"node":{"id":"4c2e96a9-4c71-57e3-b8e4-220b2cdf3002","fields":{"slug":"/[CS]Heap/"},"frontmatter":{"title":"📂 Heap"}}},{"node":{"id":"44f6bf8c-3235-5ff7-9d5f-f7c816218acb","fields":{"slug":"/[CS]Trie/"},"frontmatter":{"title":"📂 Trie(트라이)"}}},{"node":{"id":"81b508c5-2689-54e4-b36f-2e8bd07e2297","fields":{"slug":"/[CS]Tree/"},"frontmatter":{"title":"📂 Tree(트리)"}}},{"node":{"id":"2efb6036-05bf-594e-b9b5-03e399be6f78","fields":{"slug":"/[CS]이진탐색트리/"},"frontmatter":{"title":"📂 이진탐색트리(Binary Search Tree)"}}},{"node":{"id":"d1ac08af-f215-5176-9cd8-df1760f4ff7e","fields":{"slug":"/[CS]BTree&B+Tree/"},"frontmatter":{"title":"📂 B Tree & B+ Tree"}}}]},"previous":{"fields":{"slug":"/[Programmers]17684/"},"frontmatter":{"title":"👩‍💻 17684. 압축"}},"next":{"fields":{"slug":"/[Programmers]17686/"},"frontmatter":{"title":"👩‍💻 17686. 파일명 정렬"}}},"pageContext":{"id":"81b508c5-2689-54e4-b36f-2e8bd07e2297","series":"📂 Computer Science","previousPostId":"455b88fc-837a-51fe-8cfe-89ebf8a31d5e","nextPostId":"70dd5a97-abc2-56d4-89c7-71418d1114a8"}},"staticQueryHashes":[]}